home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / vim / src / digraph.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  8KB  |  361 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Read the file "credits.txt" for a list of people who contributed.
  6.  * Read the file "uganda.txt" for copying and usage conditions.
  7.  */
  8.  
  9. #ifdef DIGRAPHS
  10. /*
  11.  * digraph.c: code for digraphs
  12.  */
  13.  
  14. #include "vim.h"
  15. #include "globals.h"
  16. #include "proto.h"
  17. #include "param.h"
  18.  
  19. static void printdigraph __ARGS((char_u *));
  20.  
  21. char_u    (*digraphnew)[3];            /* pointer to added digraphs */
  22. int        digraphcount = 0;            /* number of added digraphs */
  23.  
  24. #ifdef MSDOS
  25. char_u    digraphdefault[][3] =         /* standard MSDOS digraphs */
  26.        {{'C', ',', 128},    /* € */
  27.         {'u', '"', 129},    /*  */
  28.         {'e', '\'', 130},    /* ‚ */
  29.         {'a', '^', 131},    /* ƒ */
  30.         {'a', '"', 132},    /* „ */
  31.         {'a', '`', 133},    /* … */
  32.         {'a', '@', 134},    /* † */
  33.         {'c', ',', 135},    /* ~G (SAS C can't handle the real char) */
  34.         {'e', '^', 136},    /* ~H (SAS C can't handle the real char) */
  35.         {'e', '"', 137},    /* ‰ */
  36.         {'e', '`', 138},    /* Š */
  37.         {'i', '"', 139},    /* ‹ */
  38.         {'i', '^', 140},    /* Œ */
  39.         {'i', '`', 141},    /*  */
  40.         {'A', '"', 142},    /* Ž */
  41.         {'A', '@', 143},    /*  */
  42.         {'E', '\'', 144},    /*  */
  43.         {'a', 'e', 145},    /* ‘ */
  44.         {'A', 'E', 146},    /* ’ */
  45.         {'o', '^', 147},    /* “ */
  46.         {'o', '"', 148},    /* ” */
  47.         {'o', '`', 149},    /* • */
  48.         {'u', '^', 150},    /* – */
  49.         {'u', '`', 151},    /* — */
  50.         {'y', '"', 152},    /* ˜ */
  51.         {'O', '"', 153},    /* ™ */
  52.         {'U', '"', 154},    /* š */
  53.         {'c', '|', 155},    /* › */
  54.         {'$', '$', 156},    /* œ */
  55.         {'Y', '-', 157},    /* ~] (SAS C can't handle the real char) */
  56.         {'P', 't', 158},    /* ž */
  57.         {'f', 'f', 159},    /* Ÿ */
  58.         {'a', '\'', 160},    /*   */
  59.         {'i', '\'', 161},    /* ¡ */
  60.         {'o', '\'', 162},    /* ¢ */
  61.         {'u', '\'', 163},    /* xx (SAS C can't handle the real char) */
  62.         {'n', '~', 164},    /* ¤ */
  63.         {'N', '~', 165},    /* ¥ */
  64.         {'a', 'a', 166},    /* ¦ */
  65.         {'o', 'o', 167},    /* § */
  66.         {'~', '?', 168},    /* ¨ */
  67.         {'-', 'a', 169},    /* © */
  68.         {'a', '-', 170},    /* ª */
  69.         {'1', '2', 171},    /* « */
  70.         {'1', '4', 172},    /* ¬ */
  71.         {'~', '!', 173},    /* ­ */
  72.         {'<', '<', 174},    /* ® */
  73.         {'>', '>', 175},    /* ¯ */
  74.  
  75.         {'s', 's', 225},    /* á */
  76.         {'j', 'u', 230},    /* æ */
  77.         {'o', '/', 237},    /* í */
  78.         {'+', '-', 241},    /* ñ */
  79.         {'>', '=', 242},    /* ò */
  80.         {'<', '=', 243},    /* ó */
  81.         {':', '-', 246},    /* ö */
  82.         {'~', '~', 247},    /* ÷ */
  83.         {'~', 'o', 248},    /* ø */
  84.         {'2', '2', 253},    /* ý */
  85.         {NUL, NUL, NUL}
  86.         };
  87.  
  88. #else    /* MSDOS */
  89.  
  90. char_u    digraphdefault[][3] =         /* standard ISO digraphs */
  91.        {{'~', '!', 161},    /* ¡ */
  92.         {'c', '|', 162},    /* ¢ */
  93.         {'$', '$', 163},    /* £ */
  94.         {'o', 'x', 164},    /* ¤ */
  95.         {'Y', '-', 165},    /* ¥ */
  96.         {'|', '|', 166},    /* ¦ */
  97.         {'p', 'a', 167},    /* § */
  98.         {'"', '"', 168},    /* ¨ */
  99.         {'c', 'O', 169},    /* © */
  100.         {'a', '-', 170},    /* ª */
  101.         {'<', '<', 171},    /* « */
  102.         {'-', ',', 172},    /* ¬ */
  103.         {'-', '-', 173},    /* ­ */
  104.         {'r', 'O', 174},    /* ® */
  105.         {'-', '=', 175},    /* ¯ */
  106.         {'~', 'o', 176},    /* ° */
  107.         {'+', '-', 177},    /* ± */
  108.         {'2', '2', 178},    /* ² */
  109.         {'3', '3', 179},    /* ³ */
  110.         {'\'', '\'', 180},    /* ´ */
  111.         {'j', 'u', 181},    /* µ */
  112.         {'p', 'p', 182},    /* ¶ */
  113.         {'~', '.', 183},    /* · */
  114.         {',', ',', 184},    /* ¸ */
  115.         {'1', '1', 185},    /* ¹ */
  116.         {'o', '-', 186},    /* º */
  117.         {'>', '>', 187},    /* » */
  118.         {'1', '4', 188},    /* ¼ */
  119.         {'1', '2', 189},    /* ½ */
  120.         {'3', '4', 190},    /* ¾ */
  121.         {'~', '?', 191},    /* ¿ */
  122.         {'A', '`', 192},    /* À */
  123.         {'A', '\'', 193},    /* Á */
  124.         {'A', '^', 194},    /* Â */
  125.         {'A', '~', 195},    /* Ã */
  126.         {'A', '"', 196},    /* Ä */
  127.         {'A', '@', 197},    /* Å */
  128.         {'A', 'E', 198},    /* Æ */
  129.         {'C', ',', 199},    /* Ç */
  130.         {'E', '`', 200},    /* È */
  131.         {'E', '\'', 201},    /* É */
  132.         {'E', '^', 202},    /* Ê */
  133.         {'E', '"', 203},    /* Ë */
  134.         {'I', '`', 204},    /* Ì */
  135.         {'I', '\'', 205},    /* Í */
  136.         {'I', '^', 206},    /* Î */
  137.         {'I', '"', 207},    /* Ï */
  138.         {'D', '-', 208},    /* Ð */
  139.         {'N', '~', 209},    /* Ñ */
  140.         {'O', '`', 210},    /* Ò */
  141.         {'O', '\'', 211},    /* Ó */
  142.         {'O', '^', 212},    /* Ô */
  143.         {'O', '~', 213},    /* Õ */
  144.         {'O', '"', 214},    /* Ö */
  145.         {'/', '\\', 215},    /* × */
  146.         {'O', '/', 216},    /* Ø */
  147.         {'U', '`', 217},    /* Ù */
  148.         {'U', '\'', 218},    /* Ú */
  149.         {'U', '^', 219},    /* Û */
  150.         {'U', '"', 220},    /* Ü */
  151.         {'Y', '\'', 221},    /* Ý */
  152.         {'I', 'p', 222},    /* Þ */
  153.         {'s', 's', 223},    /* ß */
  154.         {'a', '`', 224},    /* à */
  155.         {'a', '\'', 225},    /* á */
  156.         {'a', '^', 226},    /* â */
  157.         {'a', '~', 227},    /* ã */
  158.         {'a', '"', 228},    /* ä */
  159.         {'a', '@', 229},    /* å */
  160.         {'a', 'e', 230},    /* æ */
  161.         {'c', ',', 231},    /* ç */
  162.         {'e', '`', 232},    /* è */
  163.         {'e', '\'', 233},    /* é */
  164.         {'e', '^', 234},    /* ê */
  165.         {'e', '"', 235},    /* ë */
  166.         {'i', '`', 236},    /* ì */
  167.         {'i', '\'', 237},    /* í */
  168.         {'i', '^', 238},    /* î */
  169.         {'i', '"', 239},    /* ï */
  170.         {'d', '-', 240},    /* ð */
  171.         {'n', '~', 241},    /* ñ */
  172.         {'o', '`', 242},    /* ò */
  173.         {'o', '\'', 243},    /* ó */
  174.         {'o', '^', 244},    /* ô */
  175.         {'o', '~', 245},    /* õ */
  176.         {'o', '"', 246},    /* ö */
  177.         {':', '-', 247},    /* ÷ */
  178.         {'o', '/', 248},    /* ø */
  179.         {'u', '`', 249},    /* ù */
  180.         {'u', '\'', 250},    /* ú */
  181.         {'u', '^', 251},    /* û */
  182.         {'u', '"', 252},    /* ü */
  183.         {'y', '\'', 253},    /* ý */
  184.         {'i', 'p', 254},    /* þ */
  185.         {'y', '"', 255},    /* ÿ */
  186.         {NUL, NUL, NUL}
  187.         };
  188. #endif    /* MSDOS */
  189.  
  190. /*
  191.  * handle digraphs after typing a character
  192.  */
  193.     int
  194. dodigraph(c)
  195.     int        c;
  196. {
  197.     static int    backspaced;        /* character before BS */
  198.     static int    lastchar;        /* last typed character */
  199.  
  200.     if (c == -1)                /* init values */
  201.     {
  202.         backspaced = -1;
  203.     }
  204.     else if (p_dg)
  205.     {
  206.         if (backspaced >= 0)
  207.             c = getdigraph(backspaced, c, FALSE);
  208.         backspaced = -1;
  209.         if (c == BS && lastchar >= 0)
  210.             backspaced = lastchar;
  211.     }
  212.     lastchar = c;
  213.     return c;
  214. }
  215.  
  216. /*
  217.  * lookup the pair char1, char2 in the digraph tables
  218.  * if no match, return char2
  219.  */
  220.     int
  221. getdigraph(char1, char2, meta)
  222.     int    char1;
  223.     int    char2;
  224.     int    meta;
  225. {
  226.     int        i;
  227.     int        retval;
  228.  
  229.     retval = 0;
  230.     for (i = 0; ; ++i)            /* search added digraphs first */
  231.     {
  232.         if (i == digraphcount)    /* end of added table, search defaults */
  233.         {
  234.             for (i = 0; digraphdefault[i][0] != 0; ++i)
  235.                 if (digraphdefault[i][0] == char1 && digraphdefault[i][1] == char2)
  236.                 {
  237.                     retval = digraphdefault[i][2];
  238.                     break;
  239.                 }
  240.             break;
  241.         }
  242.         if (digraphnew[i][0] == char1 && digraphnew[i][1] == char2)
  243.         {
  244.             retval = digraphnew[i][2];
  245.             break;
  246.         }
  247.     }
  248.  
  249.     if (retval == 0)            /* digraph deleted or not found */
  250.     {
  251.         if (char1 == ' ' && meta)        /* <space> <char> --> meta-char */
  252.             return (char2 | 0x80);
  253.         return char2;
  254.     }
  255.     return retval;
  256. }
  257.  
  258. /*
  259.  * put the digraphs in the argument string in the digraph table
  260.  * format: {c1}{c2} char {c1}{c2} char ...
  261.  */
  262.     void
  263. putdigraph(str)
  264.     char_u *str;
  265. {
  266.     int        char1, char2, n;
  267.     char_u    (*newtab)[3];
  268.     int        i;
  269.  
  270.     while (*str)
  271.     {
  272.         skipspace(&str);
  273.         if ((char1 = *str++) == 0 || (char2 = *str++) == 0)
  274.             return;
  275.         if (char1 == ESC || char2 == ESC)
  276.         {
  277.             EMSG("Escape not allowed in digraph");
  278.             return;
  279.         }
  280.         skipspace(&str);
  281.         if (!isdigit(*str))
  282.         {
  283.             emsg(e_number);
  284.             return;
  285.         }
  286.         n = getdigits(&str);
  287.         if (digraphnew)        /* search the table for existing entry */
  288.         {
  289.             for (i = 0; i < digraphcount; ++i)
  290.                 if (digraphnew[i][0] == char1 && digraphnew[i][1] == char2)
  291.                 {
  292.                     digraphnew[i][2] = n;
  293.                     break;
  294.                 }
  295.             if (i < digraphcount)
  296.                 continue;
  297.         }
  298.         newtab = (char_u (*)[3])alloc(digraphcount * 3 + 3);
  299.         if (newtab)
  300.         {
  301.             memmove((char *)newtab, (char *)digraphnew, (size_t)(digraphcount * 3));
  302.             free(digraphnew);
  303.             digraphnew = newtab;
  304.             digraphnew[digraphcount][0] = char1;
  305.             digraphnew[digraphcount][1] = char2;
  306.             digraphnew[digraphcount][2] = n;
  307.             ++digraphcount;
  308.         }
  309.     }
  310. }
  311.  
  312.     void
  313. listdigraphs()
  314. {
  315.     int        i;
  316.  
  317.     printdigraph(NULL);
  318.     msg_start();
  319.     msg_outchar('\n');
  320.     for (i = 0; digraphdefault[i][0] && !got_int; ++i)
  321.     {
  322.         if (getdigraph(digraphdefault[i][0], digraphdefault[i][1], FALSE) == digraphdefault[i][2])
  323.             printdigraph(digraphdefault[i]);
  324.         breakcheck();
  325.     }
  326.     for (i = 0; i < digraphcount && !got_int; ++i)
  327.     {
  328.         printdigraph(digraphnew[i]);
  329.         breakcheck();
  330.     }
  331.     msg_outchar('\n');
  332.     wait_return(TRUE);        /* clear screen, because some digraphs may be wrong,
  333.                              * in which case we messed up NextScreen */
  334. }
  335.  
  336.     static void
  337. printdigraph(p)
  338.     char_u *p;
  339. {
  340.     char_u        buf[9];
  341.     static int    len;
  342.  
  343.     if (p == NULL)
  344.         len = 0;
  345.     else if (p[2] != 0)
  346.     {
  347.         if (len > Columns - 11)
  348.         {
  349.             msg_outchar('\n');
  350.             len = 0;
  351.         }
  352.         if (len)
  353.             msg_outstr((char_u *)"   ");
  354.         sprintf((char *)buf, "%c%c %c %3d", p[0], p[1], p[2], p[2]);
  355.         msg_outstr(buf);
  356.         len += 11;
  357.     }
  358. }
  359.  
  360. #endif /* DIGRAPHS */
  361.